//           gui.cpp
//  Sun May 3 17:54:13 2009
//  Copyright  2009  Jorge Eduardo Cardona Gaviria
//  <jorgeecardona@gmail.com>

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Library General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA

#include "gui.hpp"

Gui::Gui(Glib::RefPtr<Gnome::Glade::Xml> refXml)
{
	/* Main Window */
	main_window = NULL;
	refXml->get_widget("main_window", main_window);
	main_window->maximize();
	
	/* Connect signals */
	new_task_toolbutton = NULL;
	refXml->get_widget("new_task_toolbutton", new_task_toolbutton);
	new_task_toolbutton->signal_clicked().connect( sigc::mem_fun(*this,&Gui::on_new_task_toolbutton_clicked) );
	
	start_task_toolbutton = NULL;
	refXml->get_widget("start_task_toolbutton", start_task_toolbutton);
	start_task_toolbutton->signal_clicked().connect( sigc::mem_fun(*this,&Gui::on_start_task_toolbutton_clicked) );

	stop_task_toolbutton = NULL;
	refXml->get_widget("stop_task_toolbutton", stop_task_toolbutton);
	stop_task_toolbutton->signal_clicked().connect( sigc::mem_fun(*this,&Gui::on_stop_task_toolbutton_clicked) );

	stop_all_task_toolbutton = NULL;
	refXml->get_widget("stop_all_task_toolbutton", stop_all_task_toolbutton);
	stop_all_task_toolbutton->signal_clicked().connect( sigc::mem_fun(*this,&Gui::on_stop_all_task_toolbutton_clicked) );	
	
	/* Set Data Model */
	treeview_tasks = NULL;
	refXml->get_widget("treeview_tasks", treeview_tasks);
	task_columns = new TaskColumns();
	task_store = Gtk::TreeStore::create(*task_columns);
	treeview_tasks->set_model(task_store);
	treeview_tasks->append_column_editable("Name", task_columns->name);
	treeview_tasks->append_column("Elapsed", task_columns->elapsed);
	treeview_tasks->get_column(0)->set_resizable(true);
	treeview_tasks->get_column(1)->set_resizable(true);
	
//	task_store_active = Gtk::TreeModelFilter::create(task_store);
//	task_store_active->set_visible_func(&Gui::task_active);
		
	/* Create refresh timers timer */
	sigc::slot<bool> refresh_timers_slot = sigc::bind(sigc::mem_fun(*this, &Gui::refresh_timers),1);
	sigc::connection conn = Glib::signal_timeout().connect(refresh_timers_slot,1000);
}


void Gui::on_new_task_toolbutton_clicked()
{
	// Add a new task.
	Gtk::TreeModel::iterator iter = task_store->append();
	Gtk::TreeModel::Row row = *iter;
	
	Glib::Timer *t = new Glib::Timer();
	t->stop();
	
	row[task_columns->timer] = t;
	row[task_columns->name] = Glib::ustring("Poner nombre");
	row[task_columns->elapsed] = Glib::ustring("00:00:00");
}

void Gui::on_start_task_toolbutton_clicked()
{
	Glib::RefPtr<Gtk::TreeView::Selection> s = treeview_tasks->get_selection();
	
	if(s)
	{
		Gtk::TreeModel::iterator iter = s->get_selected();
		if(iter)
		{
			Gtk::TreeModel::Row row = *iter;
			
			Glib::Timer *t = row[task_columns->timer];
			t->start();
		
		}
	}
}

void Gui::on_stop_task_toolbutton_clicked()
{
	Glib::RefPtr<Gtk::TreeView::Selection> s = treeview_tasks->get_selection();
	
	if(s)
	{
		Gtk::TreeModel::iterator iter = s->get_selected();
		if(iter)
		{
			Gtk::TreeModel::Row row = *iter;
			
			Glib::Timer *t = row[task_columns->timer];
			t->stop();
		
		}
	}
}

void Gui::on_stop_all_task_toolbutton_clicked()
{
	Glib::RefPtr<Gtk::TreeView::Selection> s = treeview_tasks->get_selection();
	
	if(s)
	{
		Gtk::TreeModel::iterator iter = s->get_selected();
		if(iter)
		{
			Gtk::TreeModel::Row row = *iter;
			
			Glib::Timer *t = row[task_columns->timer];
			t->stop();
		
		}
	}
}


bool Gui::refresh_timers(int i)
{
	task_store->foreach_iter(sigc::mem_fun(*this, &Gui::refresh_timer));
	std::cout << "Refresh\n" ;	
}

bool Gui::refresh_timer(const Gtk::TreeModel::const_iterator& iter)
{
	Gtk::TreeModel::Row row = *iter;

	std::stringstream out;
	Glib::Timer *t = row[task_columns->timer];
	
	long e = static_cast<long>(t->elapsed());
	
	if ((e/3600) < 10) out << "0";
	out << (e/3600) << ":";
	
	if (((e/60)%60) < 10) out << "0";
	out << ((e/60)%60) << ":";
	
	if ((e%60) < 10) out << "0";
	out << (e%60);
	
	row[task_columns->elapsed] = out.str();
	
	return false;
}
